<template>
	<view class="uiwu">
		<uiwu-list loading="noMore" v-if="true">
			<view class="uiwu-deposit">
				<view class="uiwu-deposit-item uiwu-flex-align" v-for="(item,index) in listData" :key="index">
					<radio color="#EF821E" :checked="item.checked" :value="String(index)" @tap="changeRadio(item)" />
					<image :src="item.product_image" mode="aspectFill"></image>
					<view class="in">
						<view class="title">{{item.product_name}}</view>
						<view class="tips">{{getDay(item.expire_time)}}天后过期</view>
						<view class="time">{{uiwu.formatTimestamp(item.add_time * 1000)}}</view>
					</view>
				</view>
			</view>
		</uiwu-list>
		<view v-else class="uiwu-noteDate uiwu-flex-column">
			<image src="/static/notedata.png" mode="widthFix"></image>
			<text class="tips">暂无可兑换奖品</text>
			<view class="btn uiwu-flex-center">去抓零食</view>
		</view>
		<uiwu-footer height="104rpx">
			<view class="uiwu-footer">
				<view class="uiwu-footer-box uiwu-flex-space">
					<view class="uiwu-flex-space" style="flex: 1;">
						<label class="uiwu-footer-radio uiwu-flex-align" @tap="allChangeRadio">
							<radio color="#EF821E" @tap.stop="allChangeRadio" :checked="allAIsTrue" /><text>全选</text>
						</label>
						<view class="price">合计：<text>{{totalIn}}积分</text></view>
					</view>
					<text class="uiwu-flex-center" @tap="exchangeFun">积分兑换</text>
				</view>
			</view>
		</uiwu-footer>
		<uiwu-popup-model v-model="showModel" @confirm="confirm">
			<text>是否确认兑换{{totalIn}}积分</text>
		</uiwu-popup-model>
	</view>
</template>

<script setup lang="ts">
	import { onLoad } from '@dcloudio/uni-app';
	import { inject, ref } from 'vue';
	import { prizelist , redeemIntegral } from '@/api/prize';
	const uiwu = inject<any>('uiwu');
	const showModel = ref<boolean>(false)
	const listData = ref<Array<object>>([])
	const pageNum = ref<number>(1)
	const more = ref<string>('loading');
	const isLock = ref<boolean>(true)
	const totalIn = ref(0)
	const allAIsTrue = ref(false)
	const getprizelist = async (mack:boolean=false) : Promise<any> =>{
		if(!mack) pageNum.value = 1
		const { data:{data , total}} = await prizelist({
			page:pageNum.value,
			state:0
		});
		
		isLock.value = true
		uiwu.setlist({
			isMore:mack,
			listData,
			total,
			listRow:data,
			more,
			pageNum
		})
	}
	/**
	 * 选中某个商品
	 */
	const changeRadio = (item) => {
		item.checked = !item.checked
		allAIsTrue.value = listData.value.every(item => item.checked === true);
		totalIn.value = listData.value.filter(item => item.checked === true)
		.map(item => parseInt(item.give_integral))
		.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
	}
	/**
	 * 全选
	 */
	const allChangeRadio = () => {
		listData.value.forEach((item)=>{
			item.checked = allAIsTrue.value ? false : true
			
		})
		allAIsTrue.value = listData.value.every(item => item.checked === true);
		totalIn.value = listData.value.filter(item => item.checked === true)
		.map(item => parseInt(item.give_integral))
		.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
	}
	const exchangeFun = () => {
		if(totalIn.value == 0) return uiwu.toast('请选择奖品')
		showModel.value = true
	}
	/**
	 * 获取到期时间
	 */
	const getDay = (time) => {
		const currentTimestamp = Math.floor(Date.now() / 1000);
		const daysDifference  = time - currentTimestamp
		return Math.floor( daysDifference / (60 * 60 * 24)) || 0
	}
	const confirm = async () : Promise<any> => {
		const ids = listData.value.filter(item => item.checked === true).map(item => item.id)
		const result = await redeemIntegral({grab_prize_id:ids})
		if(!result) return 
		showModel.value = false
		uiwu.toast('兑换成功')
		getprizelist()
	}
	onLoad(()=>{
		getprizelist()
	})
</script>

<style lang="scss">
	page{
		background-color: #f6f6f6;
	}
	.uiwu-noteDate{
		align-items: center;
		padding-top: 270rpx;
		image{
			width: 220rpx;
		}
		.tips{
			font-size: 28rpx;
			font-weight: 500;
			color: #444444;
			margin-top: 10rpx;
		}
		.btn{
			width: 300rpx;
			height: 80rpx;
			background: #EF821E;
			border-radius: 40rpx;
			font-size: 32rpx;
			font-weight: bold;
			color: #FFFFFF;
			margin-top: 40rpx;
		}
	}
	.uiwu{
		padding-top: 20rpx;
	}
	.uiwu-deposit{
		background-color: #fff;
		&-item{
			padding: 24rpx;
			border-bottom: 1rpx solid #e6e6e6;
			&:last-child{
				border-bottom: 0;
			}
		}
		radio{
			transform: scale(0.8);
		}
		image{
			width: 136rpx;
			height: 136rpx;
			border-radius: 16rpx;
			margin: 0 24rpx;
		}
		.in{
			flex: 1;
			.title{
				font-size: 28rpx;
				font-weight: 800;
				color: #231815;
			}
			.tips{
				margin: 10rpx 0 10rpx;
				display: inline-block;
				padding: 4rpx 10rpx;
				height: 40rpx;
				border-radius: 8rpx;
				border: 1rpx solid #E6E6E6;
				font-size: 24rpx;
				font-weight: 500;
				color: #444444;
			}
			.time{
				font-size: 24rpx;
				font-weight: 300;
				color: #999999;
			}
		}
	}
	.uiwu-list{
		padding: 0 24rpx;
		background-color: #fff;
		&-item{
			padding: 24rpx 0;
			image{
				width: 136rpx;
				height: 136rpx;
				border-radius: 16rpx;
				margin-right: 24rpx;
			}
			.in{
				flex: 1;
				height: 136rpx;
				justify-content: space-around;
			}
			.title{
				text{
					font-size: 28rpx;
					font-weight: 800;
					color: #231815;
					&:nth-child(2){
						color: #EF821E;	
						font-weight: 400;
					}
				}
			}
			.jifen{
				font-size: 24rpx;
				font-weight: 500;
				color: #444444;
			}
			.time{
				font-size: 24rpx;
				font-weight: 300;
				color: #999999;
			}
		}
	}
	.uiwu-list-scroll{
		width: 100%;
		margin-top: 20rpx;
		height: calc(100vh - 102rpx);
		/* #ifdef H5 */
		height: calc(100vh - 102rpx - 44px);
		/* #endif */
		&.active{
			height: calc(100vh - 102rpx - 196rpx);
			/* #ifdef H5 */
			height: calc(100vh - 102rpx - 196rpx - 44px);
			/* #endif */
		}
		
	}
	.uiwu-footer{
		width: 100%;
		padding: 0 24rpx;
		background-color: #fff;
		&-box{
			padding: 16rpx 0;
			>text{
				width: 220rpx;
				height: 72rpx;
				border-radius: 36rpx;
				border: 3rpx solid #EF821E;
				font-size: 32rpx;
				font-weight: bold;
				color: #EF821E;
				margin-left: 32rpx;
				&:nth-child(2){
					background-color: #EF821E;
					color: #FFF;
				}
			}
			.price{
				font-size: 28rpx;
				font-weight: 400;
				text{
					font-weight: bold;
					color: #FF5100;
				}
			}
		}
	}
	.uiwu-footer-radio{
		radio{
			transform: scale(0.8);
		}
		text{
			font-size: 28rpx;
			font-weight: bold;
			color: #231815;
		}
	}
</style>
